package com.ruoyi.wvp.utils;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jakarta.servlet.http.HttpServletRequest;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.*;

public class JwtUtil {

    private static final String SECRET="3ddc30c87007583a96ae5bc6376b8a5b";
    public static Claims parseToken(String token, String secret) {
        return Jwts
                .parser()
                .setSigningKey(secret.getBytes())
                .parseClaimsJws(token)
                .getBody();
    }

    public static String createToken(Map<String, Object> claims, String secret,Date expiration) {
        return Jwts.builder()
                .setHeaderParam("type","JWT")
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, secret.getBytes())
                .setExpiration(expiration).compact();
    }

    public static String getToken(Map<String, Object> map) {
        //设置内容
        JwtBuilder jwtBuilder = Jwts.builder()
                .setClaims(map)
                .setHeaderParam("type","JWT");
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND, 100);
        //设置过期时间, 100s后过期
        jwtBuilder.setExpiration(instance.getTime());
        String token = jwtBuilder.signWith(SignatureAlgorithm.HS256, SECRET.getBytes()).compact();
        return token;
    }


    /**
     * 获取请求token
     *
     * @param request
     * @return token
     */
    public static  String getToken(HttpServletRequest request) {
        String token = request.getHeader(Constants.API_NOTIFY_HEADER);
        if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
            token = token.replace(Constants.TOKEN_PREFIX, "");
        }
        return token;
    }




    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.HOUR_OF_DAY, 12);
        Date time = calendar.getTime();
        String key ="3ddc30c87007583a96ae5bc6376b8a5b";
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("key","3ddc30c87007583a96ae5bc6376b8a5b");
//        String token = createToken(stringObjectHashMap, key,time);
        String token = createToken(stringObjectHashMap,SECRET,time);
        System.out.println(token);
        System.out.println(parseToken(token,key));

//        String vToken="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhbGciOiJIUzUxMiIsImtleSI6IjNkZGMzMGM4NzAwNzU4M2E5NmFlNWJjNjM3NmI4YTViIiwiZXhwIjoxNzIyNjg5NzUxfQ.-rsygaSBxOSDruwHlFYQkVcOOZRK2Tw3F-NtVeRsXWAJrdOZqVw62ORSmN0kEc4EPqCx8P_f_BRUacS8qrsUtg";
//        System.out.println(parseToken(vToken,SECRET));

//        System.out.println(getToken(stringObjectHashMap));



//        String key ="3ddc30c87007583a96ae5bc6376b8a5b";
//        String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhbGciOiJIUzUxMiIsImtleSI6IjNkZGMzMGM4NzAwNzU4M2E5NmFlNWJjNjM3NmI4YTViIiwiZXhwIjoxNzIyNjg5NzUxfQ.-rsygaSBxOSDruwHlFYQkVcOOZRK2Tw3F-NtVeRsXWAJrdOZqVw62ORSmN0kEc4EPqCx8P_f_BRUacS8qrsUtg";
//        Claims claims = parseToken(token, key);
//        System.out.println(claims);
    }
}
